package com.concerneyes.mine.TabActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.concerneyes.R;
import com.concerneyes.mine.Task.AdressImpl;
import com.concerneyes.mine.Task.AdressListener;
import com.concerneyes.mine.model.MyAdressModel;
import wheelview.DialogChooseCity;

import java.util.List;

import common.AbsActivity;

/**
 * 收货人信息添加
 */
public class AdressManageActivity extends AbsActivity implements OnClickListener, AdressListener, DialogChooseCity.chooseCitySuccessListener {
    private RelativeLayout ll_adress;
    private TextView et_adress;
    private EditText et_name, et_phone, et_adress_deteil;
    private String province, city, district;
    private boolean isAdd;
    private int id = 0;
    private AdressImpl impl;
    private int intExtra = 0;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_adress_manage);
        getRightText().setVisibility(View.VISIBLE);
        getRightText().setText("保存");
        initView();
    }

    private void initView() {
        ll_adress = (RelativeLayout) findViewById(R.id.rl_adress);
        et_adress = (TextView) findViewById(R.id.tv_adress);
        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_adress_deteil = (EditText) findViewById(R.id.et_adress_deteil);
        ll_adress.setOnClickListener(this);
        impl = new AdressImpl(this, this);
        String adress_style = getIntent().getStringExtra("adress_style");
        if (adress_style.equals("add")) {
            setTitle(R.string.add_adress);
            isAdd = true;
        } else {
            setTitle(R.string.change_adress);
            intExtra = getIntent().getIntExtra("adress_position", 0);
            impl.listAdress();
            isAdd = false;
        }
    }

    private void setAdress(MyAdressModel myModel) {
        if (myModel != null) {
            et_adress.setText(myModel.getProvince() + "-" + myModel.getDistrict() + "-" + myModel.getCity());
            et_name.setText(myModel.getName());
            et_phone.setText(myModel.getPhone1());
            et_adress_deteil.setText(myModel.getAddress());
            province = myModel.getProvince();
            city = myModel.getCity();
            district = myModel.getDistrict();
            id = myModel.getId();
        }
    }

    @Override
    public void onRightTextAction(View v) {
        super.onRightTextAction(v);
        addAdress();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 400 && resultCode == 200) {
            province = (String) data.getExtras().get("mCurrentProviceName");
            city = (String) data.getExtras().get("mCurrentCityName");
            district = (String) data.getExtras().get("mCurrentDistrictName");
            et_adress.setText(province + " - " + city + " - " + district);
        }
    }

    private void addAdress() {
        if (TextUtils.isEmpty(et_name.getText().toString())) {
            showToast("姓名");
            return;
        }
        if (TextUtils.isEmpty(et_phone.getText().toString())) {
            showToast("手机号码");
            return;
        }
        if (TextUtils.isEmpty(et_adress.getText().toString())) {
            showToast("地区信息");
            return;
        }
        if (TextUtils.isEmpty(et_adress_deteil.getText().toString())) {
            showToast("详细地址");
            return;
        }
        impl.addAdress(isAdd, String.valueOf(id), province, city, district, et_adress_deteil.getText().toString(), et_name.getText().toString(),
                et_phone.getText().toString());
    }

    @Override
    public void adressListSuccess(List<MyAdressModel> model) {
        setAdress(model.get(intExtra));
    }

    @Override
    public void adressListFail() {

    }

    @Override
    public void deleteAdressSuccess(List<MyAdressModel> model) {

    }

    @Override
    public void deleteAdressFail() {

    }

    @Override
    public void setDefaultAdressSuccess(List<MyAdressModel> model) {

    }

    @Override
    public void setDefaultAdressFail() {

    }

    @Override
    public void addAdressSuccess() {
        finish();
    }

    @Override
    public void addAdressFail() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_adress:
                int width = this.getWindowManager().getDefaultDisplay().getWidth();
                DialogChooseCity chooseCity = new DialogChooseCity(this, this, width * 5 / 6);
                chooseCity.dialogShow();
                break;

            default:
                break;
        }
    }

    @Override
    public void onChooseCitySuccess(String mCurrentProviceName, String mCurrentCityName, String resmCurrentDistrictNameultStatus) {
        province = mCurrentProviceName;
        city = mCurrentCityName;
        district = resmCurrentDistrictNameultStatus;
        et_adress.setText(province + " - " + city + " - " + district);
    }

    @Override
    public void onChooseCityFail() {

    }
}
